$(document).ready(function() {

	//统计附近警务服务站和警员的总数
	$(".all-police-station").text($(".police-station-list li").length + "个");
	$(".all-police-person").text($(".police-person-list li").length + "个");

	//自定义派警选中多选框
	$(".map-police-list li").on("click", ".check-button", function() {
		$(this).toggleClass("checked-button-bg");
		$(".police-person-num").text($(".check-button input[name = 'police-person']:checked").length);
		$(".police-station-num").text($(".check-button input[name = 'police-station']:checked").length);
	});

	//tab切换警务站和警员
	$(".hj-nearby-div div").on("click", function() {
		var nearbyIndex = $(this).index();
		$(".map-police-list").css("display", "none");
		$(".map-police-list").eq(nearbyIndex).css("display", "block");
		if(nearbyIndex == 0) {
			$(".nearby-history").text("附近警务机构");
		} else {
			$(".nearby-history").text("附近警员");
		}
	});

	//自定义下拉框列表相关点击事件
	$(".my-select").on("click", function(e) {
		e.stopPropagation();
		$(this).parent().find(".my-select-list-box").css("display", "block");
	});

	//点击确定按钮收起下拉列表
	$(".select-submit-btn-box").on("click", ".button1", function() {
		$(this).parents(".my-select-list-box").css("display", "none");
	});

	//自定义下拉框选择
	$(".my-select-list li").on("click", ".check-button", function(e) {
		e.stopPropagation();
		$(this).toggleClass("select-checked-bg");
	});

	//点击自定义下拉框每一行事件
	$(".my-select-list").on("click", "li", function(e) {
		e.stopPropagation();

		$(this).children(".check-button").toggleClass("select-checked-bg");

		if($(this).find("input[type='checkbox']").is(":checked")) {

			$(this).find("input[type='checkbox']").prop("checked", false);
		} else {

			$(this).find("input[type='checkbox']").prop("checked", true);
		}
	});

	//点击空白处让下拉框隐藏
	$("body").on("click", function() {
		$(".my-select-list-box").css("display", "none");
	});

	//点击下拉框空白处阻止下拉框隐藏
	$(".my-select-list-box").on("click", function(e) {
		e.stopPropagation();
	});

	//状态选择的点击事件
	$(".state").click(function() {
		$(this).toggleClass("state-checked");
	});

	//添加警员
	$(".alert-table-box").on("click", ".add-police-person", function() {

		if($(this).text() == "添加") {
			$(this).text("已添加");
			$(this).css("color","#999");
			var person_name = $(this).parents("tr").children("td").eq(0).text();
			var phone_num = $(this).parents("tr").children("td").eq(1).text();
			$(".checked-person-name").append("<div class='name-cell'><div class='remove-checked'>×</div><span>" + person_name + "</span><span>&nbsp;" + phone_num + "</span></div>");
		}else{
			alert("该警员已选择");
		}

	});
	
	//删除选中的警员名单
	$(".checked-person-name").on("click",".remove-checked",function(){
		
		var name = $(this).parent().children().find("span").eq(0).text();
		
		for (var i = 1; i < $(".person-list-box .table1 tr").length; i++) {
			
			if($(".person-list-box .table1 tr").eq(i).children().find("td").eq(0).text() == name){
				
				$(".person-list-box .table1 tr").eq(i).children().find(".add-police-person").text("添加");
				$(".person-list-box .table1 tr").eq(i).children().find(".add-police-person").css("color","#1e9826");
			}
		}
		$(this).parent().remove();
	});
	
	//删除已选择单位
	$(".checked-company-name").on("click",".remove-checked",function(){
		$(this).parent().remove();
	});

	//查看派警人员按钮点击事件
	$(".look-dispatch-personnel-btn").click(function() {
		var display = $(".dispatch-personnel-box").css("display");
		if(display == "none") {
			$(".dispatch-personnel-box").css("display", "block");

//			var top_H = $(".dispatch-personnel-box").outerHeight() + 307;
//
//			$(".sirens-detail-box").animate({
//				"top": top_H
//			}, 500);
		} else {
//			$(".sirens-detail-box").animate({
//				"top": "307px"
//			}, 500);

			$(".dispatch-personnel-box").css("display", "none");
		}
	});

});